/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package drt;

import evaluation.Edge;
import evaluation.Graph;
import evaluation.Vertex;
import java.util.LinkedHashSet;
import java.util.Map;
import logic.Type;

/**
 *
 * @author phong
 */
public class SDRSMappingCondition extends SDRSCondition {
    
    DRSReferent ref;
    DrtExpression term;
    
    public SDRSMappingCondition(DrtExpression term, DRSReferent ref) {
        this.term = term;
        this.ref = ref;
    }
    
    public DRSReferent getRef() {
        return this.ref;
    }
    
    @Override
    public Type getType() {
        return Type.UNKNOWN_TYPE;
    }
    
    /*
     * we do not solve the double negated DRS
     * (it is not true that -(-DRS) = DRS
     */
    @Override
    public SDRSMappingCondition simplify() {
        return new SDRSMappingCondition(this.term.simplify(), this.ref);
    }
    
    @Override
    public SDRSMappingCondition replace(DrtVariable var, DrtExpression exp) {
        return new SDRSMappingCondition(this.term.replace(var, exp), this.ref);
    }
    
    @Override
    public String toString() {
        return this.ref.toString() + ":" + this.term.toString();
    }
    
    @Override
    public boolean equals(Object other) {
        if (other instanceof SDRSMappingCondition) {
            return this.term.equals(((SDRSMappingCondition)other).term) && 
                    this.ref.equals(((SDRSMappingCondition)other).ref);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return this.term.hashCode();
    }

    @Override
    public void getRefs(LinkedHashSet<DRSReferent> refs) {
        this.term.getRefs(refs);
        refs.add(this.ref);
    }
    
    @Override
    public void getAllVariables(LinkedHashSet<DrtVariable> vars) {
        vars.add(this.ref);
        this.term.getAllVariables(vars);
    }

    @Override
    public Graph toGraph() {
        
        Graph gt = this.term.toGraph();
        
        Graph g = new Graph();
        
        Vertex p = new Vertex(this.getRef().getName(), Vertex.TYPE_REF);
        Vertex l = new Vertex(Naming.getUniqueName("l0"), Vertex.TYPE_LABEL);
        Vertex opt = Vertex.createIs();
        
        g.addEdge(new Edge("", l, opt));
        g.addEdge(new Edge("", p, opt));
        g.merge(gt);

        for (Vertex v : gt.getMarks()){
            g.addEdge(new Edge("", v, l));
        }
        
        return g;
    }
    
    @Override
    public SDRSMappingCondition getTemplate(Map<String,String> map) {
        DrtExpression newTerm = this.term.getTemplate(map);
        return new SDRSMappingCondition(newTerm, ref);
    }    
}

